﻿using Hospital.IService;
using Hospital.Model.type;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class SpecialtyController : ControllerBase
    {
        private ISpecialtyService _iSpecialtyService;

        public SpecialtyController(ISpecialtyService iSpecialtyService)
        {
            _iSpecialtyService = iSpecialtyService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResult>> GetSpecialty(int id)
        {
            try
            {
                var specialty = await _iSpecialtyService.FindAsync(id);
                if (specialty != null) return ApiResultHelper.Success(specialty);
                return ApiResultHelper.Error("未找到");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("获取专业出现错误");
            }
        }

        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetSpecialtys()
        {
            try
            {
                var specialtys = await _iSpecialtyService.QueryAsync();
                if (specialtys != null) { return ApiResultHelper.Success(specialtys); }
                else
                {
                    return ApiResultHelper.Error("数据为空");
                }
            }
            catch
            {
                return ApiResultHelper.Error("获取信息出现错误");
            }
        }

        [HttpPost]
        public async Task<ActionResult<ApiResult>> CreateSpecialty([FromBody] Specialty Specialty)
        {
            try
            {
                bool b = await _iSpecialtyService.CreateAsync(Specialty);
                if (b) return ApiResultHelper.Success("服务器添加专业成功", Specialty);
                return ApiResultHelper.Error("服务器添加专业失败");
            }
            catch
            {
                return ApiResultHelper.Error("服务器添加专业时出现错误");
            }
        }

        [HttpPatch("{id}")]
        public async Task<ActionResult<ApiResult>> UpdateSpecialty(int id, [FromBody] Specialty Specialty)
        {
            try
            {
                if (id != Specialty.Id)
                {
                    return BadRequest();
                }
                else
                {
                    bool b = await _iSpecialtyService.EditAsync(Specialty);
                    if (b) return ApiResultHelper.Success("服务器修改专业成功", Specialty);
                    return ApiResultHelper.Error("服务器修改专业失败");
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("服务器修改专业出现错误");
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteSpecialty(int id)
        {
            try
            {
                bool b = await _iSpecialtyService.DeleteAsync(id);
                if (b) return ApiResultHelper.Success("删除专业成功");
                return ApiResultHelper.Error("删除专业失败");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("删除专业错误");
            }
        }
    }

}
